package com.david.andserver;

import android.os.Environment;

import com.david.andserver.util.FileUtils;
import com.david.andserver.util.PathUtil;
import com.yanzhenjie.andserver.annotation.GetMapping;
import com.yanzhenjie.andserver.annotation.PostMapping;
import com.yanzhenjie.andserver.annotation.RequestParam;
import com.yanzhenjie.andserver.annotation.RestController;
import com.yanzhenjie.andserver.http.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

@RestController
public class FileController {
    @GetMapping("/file/musics")
    String getMusics() {
        return FileUtils.musicString;
    }

    @GetMapping("/file/videos")
    String getVideos() {
        return FileUtils.videoString;
    }

    @GetMapping("/file/qurk/videos")
    String getQurkVideos() {
        return PathUtil.getQurkNetVideoData();
    }



    /**
     * 开启服务后访问：http://phoneIp:8080/upload.html 即可上传文件（assets中有上传html页面）
     * */
    @PostMapping("/file/upload")
    String upload(@RequestParam(name="file") MultipartFile multipartFile){
        File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + "/upload");
        if (!dir.exists()) {
            dir.mkdirs();
            new File(dir.getAbsolutePath() + "/image").mkdir();
            new File(dir.getAbsolutePath() + "/music").mkdir();
            new File(dir.getAbsolutePath() + "/video").mkdir();
            new File(dir.getAbsolutePath() + "/books").mkdir();
            new File(dir.getAbsolutePath() + "/other").mkdir();
        }
        try {
            String fileName = multipartFile.getFilename();
            String filePath = "";
            if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg") ||
                    fileName.endsWith(".png") || fileName.endsWith(".webp") ||
                    fileName.endsWith(".gif") || fileName.endsWith(".bmp") ||
                    fileName.endsWith(".svg")
            ) {
                filePath = dir + "/image/" + fileName;
            } else if (fileName.endsWith(".mp3") || fileName.endsWith(".flac") ||
                    fileName.endsWith(".wav") || fileName.endsWith(".ape")) {
                filePath = dir + "/music/" + fileName;

            } else if (fileName.endsWith(".mp4") || fileName.endsWith(".avi") ||
                    fileName.endsWith(".mpeg") || fileName.endsWith(".3gp") ||
                    fileName.endsWith(".mov") || fileName.endsWith(".mkv") ||
                    fileName.endsWith(".flv") || fileName.endsWith(".vob")
            ) {
                filePath = dir + "/video/" + fileName;

            } else if (fileName.endsWith(".txt") || fileName.endsWith(".pdf") ||
                    fileName.endsWith(".doc") || fileName.endsWith(".docx") ||
                    fileName.endsWith(".xlsx") || fileName.endsWith(".xls")
            ) {
                filePath = dir + "/books/" + fileName;

            } else {
                filePath = dir + "/other/" + fileName;

            }
            InputStream inputStream = (multipartFile.getStream());
            FileOutputStream fileWriter = new FileOutputStream(filePath);
            byte[] bytes = new byte[1024];
            int length;
            while ((length = inputStream.read(bytes)) > 0) {
                fileWriter.write(bytes, 0, length);
                fileWriter.flush();
            }
            inputStream.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
            return "upload failed:" + e.getMessage();
        }
        return "upload success:" + multipartFile.getFilename();
    }
}
